package menu.sys

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.alibaba.fastjson.parser.Feature
import fundation.gsonPrint
import normal.test.authJson
import normal.test.menuJson
import org.junit.Test

/**
 * @author futao
 * Created on 2017/12/3 - 14:02.
 */
class MenuSys {
    private fun toOrderedLinkedHashMap(arguments: String): LinkedHashMap<String, String> {
        return JSON.parseObject(arguments, object : TypeReference<LinkedHashMap<String, String>>() {}, Feature.OrderedField)
    }

    @Test
    fun testCase() {
        /*菜单数据*/
        val menu = menuJson
        /*角色菜单数据*/
        val auth = authJson
        val roleTypes = intArrayOf(1, 2)
        /*当前角色类型*/
        val currentRoleType = 1
        /*一级菜单数据
        * (导航菜单)
        * eg    "mall":""
        *       "marketing":""
        * */
        val menuLevelOne = toOrderedLinkedHashMap(menu)
        /*
        * 用来保存新的数据的一级菜单
        * eg "mall":""
        * */
        val roleLevelOne = LinkedHashMap<String, Any>()
        /*遍历一级菜单
        * eg "mall":""
        * */
        menuLevelOne.forEach f1@ { (k, v) ->
            kotlin.run {
                //                println("当前key==$k,当前value==$v")
                /*根据当前一级菜单拿到二级菜单
                * eg    "menu":""
                *       "menuMap":""
                * */
                val menuLevelTwo = toOrderedLinkedHashMap(v)
                /*遍历二级菜单
                * menu和menuMap
                * */
                menuLevelTwo.forEach f2@ { (key, value) ->
                    kotlin.run {
                        when (key) {
                        /*当前二级菜单为menu*/
                            "menu" -> {
                                /*用来保存二级菜单
                                * menu和menuMap
                                * */
                                val roleLevelTwoMenu = LinkedHashMap<String, Any>()
                                /*获取menu下的三级菜单
                                * eg    "route":"mall"
                                *       "name":"mall"
                                * */
                                val menuLevelThreeMenu = toOrderedLinkedHashMap(value)
                                /*用来保存menu下的三级菜单
                                * eg    "name":"mall"
                                *       "title":"商城"
                                * */
                                val roleLevelThree = LinkedHashMap<String, Any?>()
                                /*当前menu的nav为true则不隐藏*/
                                if (menuLevelThreeMenu["nav"].toString() == "true") {
                                    /*检查是否包含roleType*/
                                    val containsKeyRoleType = menuLevelThreeMenu.containsKey("roleType")
                                    /*存在roleType -> 该菜单是给某个角色专用*/
                                    if (containsKeyRoleType) {
                                        /*当前菜单的roleType不等于当前角色的roleType*/
                                        if (currentRoleType.toString() != menuLevelThreeMenu["roleType"]) {
                                            /*直接进入下一次一级菜单的循环*/
                                            return@f2
                                        }
                                        /*当前菜单的roleType与当前角色的roleType相等*/
                                        else {
                                            /*向三级菜单中添加键值对
                                            * eg    "name": "mall"
                                            *       "title": "商城"
                                            * */
                                            roleLevelThree.put("name", menuLevelThreeMenu["name"])
                                            roleLevelThree.put("title", menuLevelThreeMenu["title"])

                                            menuLevelThreeMenu["line"]?.let { roleLevelThree.put("line", it) }
                                            menuLevelThreeMenu["style"]?.let { roleLevelThree.put("style", it) }
                                            menuLevelThreeMenu["icon"]?.let { roleLevelThree.put("icon", it) }

                                            /*根据该角色原有的权限信息查询该角色对于该菜单的auth信息*/
                                            val oldRoleLevelOne = toOrderedLinkedHashMap(auth)
                                            val oldRoleLevelTwo = toOrderedLinkedHashMap(oldRoleLevelOne[k].toString())
                                            val oldRoleLevelThree = toOrderedLinkedHashMap(oldRoleLevelTwo["menu"].toString())
                                            val authArray = JSON.parseArray(oldRoleLevelThree["auth"].toString())
                                            roleLevelThree.put("auth", authArray)
                                            roleLevelThree.put("roleType", menuLevelThreeMenu["roleType"])
                                            /*向二级菜单中添加 "menu":""键值对 */
                                            roleLevelTwoMenu.put("menu", roleLevelThree)



                                            roleLevelOne.put(k, roleLevelTwoMenu)
                                            println("6666 $roleLevelThree")


                                        }
                                    }
                                    /*不存在roleType -> 该菜单是个通用菜单*/
                                    else {
                                        roleLevelThree.put("name", menuLevelThreeMenu["name"])
                                        roleLevelThree.put("title", menuLevelThreeMenu["title"])

                                        menuLevelThreeMenu["line"]?.let { roleLevelThree.put("line", it) }
                                        menuLevelThreeMenu["style"]?.let { roleLevelThree.put("style", it) }
                                        menuLevelThreeMenu["icon"]?.let { roleLevelThree.put("icon", it) }

                                        //roleLevelThree.put("auth", setOf(authView, authShow))

                                        //获取menu的auth
                                        val oldRoleLevelOne = toOrderedLinkedHashMap(auth)
                                        val oldRoleLevelTwo = toOrderedLinkedHashMap(oldRoleLevelOne[k].toString())
                                        val oldRoleLevelThree = toOrderedLinkedHashMap(oldRoleLevelTwo["menu"].toString())
                                        val authArray = JSON.parseArray(oldRoleLevelThree["auth"].toString())
                                        roleLevelThree.put("auth", authArray)
                                        /*向二级菜单中添加 "menu":""键值对 */
                                        println("7777 $roleLevelThree")

                                        roleLevelTwoMenu.put("menu", roleLevelThree)
                                        roleLevelOne.put(k, roleLevelTwoMenu)


                                    }
                                }
                                /*当前menu  的nav为false则隐藏，直接进入下一个一级菜单的遍历*/
                                else {
                                    return@f1
                                }


//                                roleLevelTwo.put("menuMap", roleLevelThreeMenuMap)
//                                println("#################$roleLevelTwo")
//                                roleLevelOne.put(k, roleLevelTwo)
                            }
                        /*当前二级菜单为menuMap*/
                            "menuMap" -> {
                                /*用来保存二级菜单
                                * menu和menuMap
                                * */
                                val roleLevelTwoMenuMap = LinkedHashMap<String, Any>()

                                /*获取menuMap下的三级菜单
                                * eg    "operation":"'
                                *       "goods":""
                                * */
                                val menuLevelThreeMenuMap = toOrderedLinkedHashMap(value)
                                /*用来保存角色的三级菜单*/
                                val roleLevelThreeMenuMap = LinkedHashMap<String, Any>()
                                /*遍历三级菜单
                                * eg   "operation":""
                                * */
                                menuLevelThreeMenuMap.forEach f3@ { (menuMap_key, menuMap_value) ->
                                    kotlin.run {
                                        /*获取当前三级菜单的四级菜单*/
                                        val menuLevelFourMenuMap = toOrderedLinkedHashMap(menuMap_value)
                                        /*用来保存角色的四级菜单*/
                                        val roleLevelFourMenuMap = LinkedHashMap<String, Any?>()
                                        /*该菜单hide*/
                                        if (menuLevelFourMenuMap["hide"].toString() == "true") {
                                            /*直接进入下一次三级菜单的循环*/
                                            return@f3
                                        }
                                        /*该菜单未hide*/
                                        else {
                                            /*该三级菜单roleType不为空*/
                                            if (menuLevelFourMenuMap.containsKey("roleType")) {
                                                /*获取当前菜单的roleType*/
                                                val menuLevelFourRoleType = menuLevelFourMenuMap["roleType"].toString()
                                                /*当前角色的roleType等于该菜单的roleType*/
                                                if (currentRoleType.toString() == menuLevelFourRoleType) {

                                                    roleLevelFourMenuMap.put("id", menuLevelFourMenuMap["id"])
                                                    roleLevelFourMenuMap.put("title", menuLevelFourMenuMap["title"])
                                                    roleLevelFourMenuMap.put("roleType", menuLevelFourMenuMap["roleType"])


                                                    if (menuLevelFourMenuMap["line"] != null) {
                                                        roleLevelFourMenuMap.put("line", menuLevelFourMenuMap["line"])
                                                    }
                                                    if (menuLevelFourMenuMap["style"] != null) {
                                                        roleLevelFourMenuMap.put("style", menuLevelFourMenuMap["style"])
                                                    }
                                                    if (menuLevelFourMenuMap["icon"] != null) {
                                                        roleLevelFourMenuMap.put("icon", menuLevelFourMenuMap["icon"])
                                                    }

                                                    //获取当前角色对该菜单的auth信息
                                                    val oldRoleLevelOne = toOrderedLinkedHashMap(auth)
                                                    val oldRoleLevelTwo = toOrderedLinkedHashMap(oldRoleLevelOne[k].toString())
                                                    val oldRoleLevelThree = toOrderedLinkedHashMap(oldRoleLevelTwo["menuMap"].toString())
                                                    val oldRoleLevelFour = toOrderedLinkedHashMap(oldRoleLevelThree[menuMap_key].toString())
                                                    val authArray = JSON.parseArray(oldRoleLevelFour["auth"].toString())
                                                    roleLevelFourMenuMap.put("auth", authArray)
                                                    /*获取当前三级菜单的的items*/
                                                    val items = JSON.parseArray(menuLevelFourMenuMap["items"].toString())
                                                    /*用来保存角色的items信息*/
                                                    val newItemArray = ArrayList<Any>()
                                                    /*遍历items子菜单*/
                                                    items?.forEach itemForEach@ { item ->
                                                        kotlin.run {
                                                            /*获取当前item的信息
                                                            * eg    "id": "mall-operation-summary"
                                                            *       "href": "#mall-operation-summary"
                                                            * */
                                                            val itemObject = toOrderedLinkedHashMap(item.toString())
                                                            /*用来保存角色的item信息*/
                                                            val itemNew = LinkedHashMap<Any, Any?>()
                                                            /*不隐藏*/
                                                            if (itemObject["hide"].toString() == "false") {
                                                                /*当前item不包含roleType*/
                                                                if (!itemObject.containsKey("roleType")) {
                                                                    /*构造item*/
                                                                    with(itemNew) {
                                                                        put("id", itemObject["id"])
                                                                        put("title", itemObject["title"])
//                                                                        put("roleType", itemObject["roleType"])

                                                                        if (itemObject["line"] != null) {
                                                                            put("line", itemObject["line"])
                                                                        }
                                                                        if (itemObject["style"] != null) {
                                                                            put("style", itemObject["style"])
                                                                        }
                                                                        if (itemObject["icon"] != null) {
                                                                            put("icon", itemObject["icon"])
                                                                        }

                                                                        //获取auth
                                                                        val oldRoleLevelOne = toOrderedLinkedHashMap(auth)
                                                                        val oldRoleLevelTwo = toOrderedLinkedHashMap(oldRoleLevelOne[k].toString())
                                                                        val oldRoleLevelThree = toOrderedLinkedHashMap(oldRoleLevelTwo["menuMap"].toString())
                                                                        val oldRoleLevelFour = toOrderedLinkedHashMap(oldRoleLevelThree[menuMap_key].toString())
                                                                        val oldRoleItems = JSON.parseArray(oldRoleLevelFour["items"].toString())

                                                                        oldRoleItems.forEach { oldRoleItem ->
                                                                            run {
                                                                                val oldRoleMenuItemMap = toOrderedLinkedHashMap(oldRoleItem.toString())
                                                                                if (oldRoleMenuItemMap["id"] == itemObject["id"]) {
                                                                                    put("auth", JSON.parseArray(oldRoleMenuItemMap["auth"].toString()))
                                                                                }
                                                                            }
                                                                        }
                                                                    }

                                                                }
                                                                /*当前item包含roleType*/
                                                                else {
                                                                    /*当前角色*/
                                                                    if (currentRoleType.toString() == itemObject["roleType"]) {
                                                                        /*构造item*/
                                                                        with(itemNew) {
                                                                            put("id", itemObject["id"])
                                                                            put("title", itemObject["title"])
                                                                            put("roleType", itemObject["roleType"])

                                                                            if (itemObject["line"] != null) {
                                                                                put("line", itemObject["line"])
                                                                            }
                                                                            if (itemObject["style"] != null) {
                                                                                put("style", itemObject["style"])
                                                                            }
                                                                            if (itemObject["icon"] != null) {
                                                                                put("icon", itemObject["icon"])
                                                                            }


                                                                            //获取auth
                                                                            val oldRoleLevelOne = toOrderedLinkedHashMap(auth)
                                                                            val oldRoleLevelTwo = toOrderedLinkedHashMap(oldRoleLevelOne[k].toString())
                                                                            val oldRoleLevelThree = toOrderedLinkedHashMap(oldRoleLevelTwo["menuMap"].toString())
                                                                            val oldRoleLevelFour = toOrderedLinkedHashMap(oldRoleLevelThree[menuMap_key].toString())
                                                                            val oldRoleItems = JSON.parseArray(oldRoleLevelFour["items"].toString())

                                                                            oldRoleItems.forEach { oldRoleItem ->
                                                                                run {
                                                                                    val oldRoleMenuItemMap = toOrderedLinkedHashMap(oldRoleItem.toString())
                                                                                    if (oldRoleMenuItemMap["id"] == itemObject["id"]) {
                                                                                        put("auth", JSON.parseArray(oldRoleMenuItemMap["auth"].toString()))
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
//                                                                        newItemArray.add(itemNew)
                                                                    }
                                                                    /*不是当前角色*/
                                                                    else {
                                                                        /*进入下一个item*/
                                                                        return@itemForEach
                                                                    }
                                                                }

                                                                /*向角色的ItemArray添加item数据*/
                                                                newItemArray.add(itemNew)
                                                            }
                                                            /*隐藏*/
                                                            else {
                                                                /*进入下一个item*/
                                                                return@itemForEach
                                                            }
                                                        }
                                                    }
                                                    if (newItemArray.count() > 0) {
                                                        roleLevelFourMenuMap.put("items", newItemArray)
                                                    }

                                                    roleLevelThreeMenuMap.put(menuMap_key, roleLevelFourMenuMap)

                                                    val any1 = roleLevelOne[k]
                                                    if (any1 != null) {
                                                        val any = any1 as LinkedHashMap<String, Any>
                                                        any.put("menuMap", roleLevelThreeMenuMap)
                                                        roleLevelOne.put(k, any)
                                                    } else {

                                                    }

                                                }
                                                /*当前角色的roleType不等于该菜单的roleType
                                                * 直接进入下一个三级菜单的遍历
                                                * */
                                                else {
                                                    return@f3
                                                }
                                            }
                                            /*该三级菜单roleType为空*/
                                            else {
                                                roleLevelFourMenuMap.put("id", menuLevelFourMenuMap["id"])
                                                roleLevelFourMenuMap.put("title", menuLevelFourMenuMap["title"])
                                                //获取当前角色对该菜单的auth信息
                                                val oldRoleLevelOne = toOrderedLinkedHashMap(auth)
                                                val oldRoleLevelTwo = toOrderedLinkedHashMap(oldRoleLevelOne[k].toString())
                                                val oldRoleLevelThree = toOrderedLinkedHashMap(oldRoleLevelTwo["menuMap"].toString())
                                                val oldRoleLevelFour = toOrderedLinkedHashMap(oldRoleLevelThree[menuMap_key].toString())
                                                val authArray = JSON.parseArray(oldRoleLevelFour["auth"].toString())
                                                roleLevelFourMenuMap.put("auth", authArray)
                                                /*获取当前三级菜单的的items*/
                                                val items = JSON.parseArray(menuLevelFourMenuMap["items"].toString())
                                                /*用来保存角色的items信息*/
                                                val newItemArray = ArrayList<Any>()
                                                /*遍历items子菜单*/
                                                items?.forEach itemForEach@ { item ->
                                                    kotlin.run {
                                                        /*获取当前item的信息
                                                        * eg    "id": "mall-operation-summary"
                                                        *       "href": "#mall-operation-summary"
                                                        * */
//                                                        println("buweinull=====$item")
                                                        val itemObject = toOrderedLinkedHashMap(item.toString())
                                                        /*用来保存角色的item信息*/
                                                        val itemNew = LinkedHashMap<Any, Any?>()
                                                        /*隐藏*/
                                                        if (itemObject["hide"].toString() == "true") {
                                                            /*进入下一个item*/
                                                            return@itemForEach
                                                        }
                                                        /*不隐藏*/
                                                        else {
                                                            /*当前item不包含roleType*/
                                                            if (!itemObject.containsKey("roleType")) {
                                                                /*构造item*/
                                                                with(itemNew) {
                                                                    put("id", itemObject["id"])
                                                                    put("title", itemObject["title"])

                                                                    //获取auth
                                                                    val oldRoleLevelOne = toOrderedLinkedHashMap(auth)
                                                                    val oldRoleLevelTwo = toOrderedLinkedHashMap(oldRoleLevelOne[k].toString())
                                                                    val oldRoleLevelThree = toOrderedLinkedHashMap(oldRoleLevelTwo["menuMap"].toString())
                                                                    val oldRoleLevelFour = toOrderedLinkedHashMap(oldRoleLevelThree[menuMap_key].toString())
                                                                    val oldRoleItems = JSON.parseArray(oldRoleLevelFour["items"].toString())

                                                                    oldRoleItems.forEach { oldRoleItem ->
                                                                        run {
                                                                            val oldRoleMenuItemMap = toOrderedLinkedHashMap(oldRoleItem.toString())
                                                                            if (oldRoleMenuItemMap["id"] == itemObject["id"]) {
                                                                                put("auth", JSON.parseArray(oldRoleMenuItemMap["auth"].toString()))
                                                                            }
                                                                        }
                                                                    }
                                                                }

                                                            }
                                                            /*当前item包含roleType*/
                                                            else {
                                                                /*当前角色*/
                                                                if (currentRoleType.toString() == itemObject["roleType"]) {
                                                                    /*构造item*/
                                                                    with(itemNew) {
                                                                        put("id", itemObject["id"])
                                                                        put("title", itemObject["title"])
                                                                        put("roleType", itemObject["roleType"])


                                                                        if (itemObject["line"] != null) {
                                                                            put("line", itemObject["line"])
                                                                        }
                                                                        if (itemObject["style"] != null) {
                                                                            put("style", itemObject["style"])
                                                                        }
                                                                        if (itemObject["icon"] != null) {
                                                                            put("icon", itemObject["icon"])
                                                                        }


                                                                        //获取auth
                                                                        val oldRoleLevelOne = toOrderedLinkedHashMap(auth)
                                                                        val oldRoleLevelTwo = toOrderedLinkedHashMap(oldRoleLevelOne[k].toString())
                                                                        val oldRoleLevelThree = toOrderedLinkedHashMap(oldRoleLevelTwo["menuMap"].toString())
                                                                        val oldRoleLevelFour = toOrderedLinkedHashMap(oldRoleLevelThree[menuMap_key].toString())
                                                                        val oldRoleItems = JSON.parseArray(oldRoleLevelFour["items"].toString())

                                                                        oldRoleItems.forEach { oldRoleItem ->
                                                                            run {
                                                                                val oldRoleMenuItemMap = toOrderedLinkedHashMap(oldRoleItem.toString())
                                                                                if (oldRoleMenuItemMap["id"] == itemObject["id"]) {
                                                                                    put("auth", JSON.parseArray(oldRoleMenuItemMap["auth"].toString()))
                                                                                }
                                                                            }
                                                                        }
                                                                    }
//                                                                        newItemArray.add(itemNew)
                                                                }
                                                                /*不是当前角色*/
                                                                else {
                                                                    /*进入下一个item*/
                                                                    return@itemForEach
                                                                }
                                                            }

                                                            /*向角色的ItemArray添加item数据*/
                                                            newItemArray.add(itemNew)
                                                        }
                                                    }
                                                }
                                                if (newItemArray.count() > 0) {
                                                    roleLevelFourMenuMap.put("items", newItemArray)
                                                }
                                                roleLevelThreeMenuMap.put(menuMap_key, roleLevelFourMenuMap)
//                                                roleLevelTwoMenuMap.put("menuMap", roleLevelThreeMenuMap)
                                                val any1 = roleLevelOne[k]
                                                if (any1 != null) {
                                                    val any = any1 as LinkedHashMap<String, Any>
                                                    any.put("menuMap", roleLevelThreeMenuMap)
                                                    roleLevelOne.put(k, any)
                                                } else {

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        /*二级菜单出现了除menu和menuMap之外的属性*/
                            else -> {
                                throw Exception("menuLevelTwo菜单格式有误")
                            }
                        }

                    }
                }
            }
        }
        gsonPrint(roleLevelOne)
    }
}